
/**
 * 复制一个新的数据，通过json序列化反序列化得到
 */
export function copyObj(obj: any) {
    if (obj === undefined || obj === null) {
        return null;
    }
    return JSON.parse(JSON.stringify(obj));
}

function copyNotEmptyObj(obj: any) {
    let cObj = copyObj(obj);
    for (let k in cObj) {
        if (cObj.hasOwnProperty(k) && !cObj[k]) {
            delete cObj[k];
        }
    }
    return cObj;
}

function copyNotEmptyArr(arr: any) {
    let cArr = copyObj(arr);
    for (let i = 0; i < cArr.length; i++) {
        if (cArr[i] instanceof Array) {
            cArr[i] = copyNotEmptyArr(cArr[i])
        } else {
            cArr[i] = copyNotEmptyObj(cArr[i])
        }
    }
    return cArr;
}

/**
 * 复制一个新的数据，不包含源对象为空的属性
 */
export function copyNotEmpty(obj: any) {
    if (obj instanceof Array) {
        return copyNotEmptyArr(obj);
    } else {
        return copyNotEmptyObj(obj);
    }
}

/**
 * 生成UUID
 */
export function getUuid() {
    let s = [];
    let hexDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (let i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
    }
    return s.join("")
}